# -*- coding: utf-8 -*-
# @Time    : 2021/9/3 14:39
# @Author  : huangwei
# @File    : functions.py
# @Software: PyCharm
import os
import sys
import time
import cv2
import numpy as np
import math
import shutil


def exist_watch_path( watch_path ):
    """判断监控目录是否存在"""
    if not watch_path:
        print("watch_path can not be no value!")
        sys.exit()
    elif os.path.exists(watch_path):
        print("found watch path:", watch_path)
    else:
        print("watch path is not exist:", watch_path)
        sys.exit()


def get_current_time():
    """取当前时间"""
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    return current_time


def psnr( file1, file2 ):
    # 计算两张图片的 psnr 值
    img1 = cv2.imread(file1)
    img2 = cv2.imread(file2)

    mse = np.mean((img1 / 255.0 - img2 / 255.0) ** 2)
    if mse < 1e-10:
        return 100

    PSNR = 20 * math.log10(1 / math.sqrt(mse))
    return PSNR


def water_psnr( WATERMARK_PATH, file ):
    # 计算两张水印的 psnr 值
    watermark = cv2.imread(WATERMARK_PATH, cv2.IMREAD_GRAYSCALE)
    watermark_shape = watermark.shape

    file = file.astype("uint8")

    file = cv2.resize(file, (watermark_shape[1], watermark_shape[0]))

    mse = np.mean((watermark / 255.0 - file / 255.0) ** 2)
    if mse < 1e-10:
        return 100

    PSNR = 20 * math.log10(1 / math.sqrt(mse))
    return PSNR


def create_dir( dir_path ):
    """创建文件夹"""
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    else:
        print("%s is exists" % dir_path)


def delete_dir( dir_path ):
    """删除文件夹"""
    if os.path.exists(dir_path):
        shutil.rmtree(dir_path)
    else:
        print("%s is not exists" % dir_path)
